package com.jiangdg.demo
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.gyf.immersionbar.ImmersionBar

class ImagePreviewActivity : AppCompatActivity() {

    private var immersionBar: ImmersionBar? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStatusBar()
        setContentView(R.layout.activity_image_preview)
        findViewById<ImageView>(R.id.ivClose).setOnClickListener { finish() }

        val pathList = intent.getStringArrayListExtra(EXTRA_FILE_PATH_LIST)
        val currentPosition = intent.getIntExtra(EXTRA_FILE_CURRENT_POSITION,0)
        val viewPager: ViewPager2 = findViewById(R.id.viewPager)
        val adapter = ImagePreviewPagerAdapter(pathList!!.toList())
        viewPager.adapter = adapter
        viewPager.currentItem = currentPosition
    }

    companion object {
        private const val EXTRA_FILE_PATH_LIST = "extra_file_path_list"
        private const val EXTRA_FILE_CURRENT_POSITION = "extra_file_current_position"

        fun createIntent(context: Context, fileList: ArrayList<String>, fileCurrentPosition:Int): Intent {
            return Intent(context, ImagePreviewActivity::class.java).apply {
                putExtra(EXTRA_FILE_CURRENT_POSITION, fileCurrentPosition)
                putStringArrayListExtra(EXTRA_FILE_PATH_LIST,fileList)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        immersionBar = null
    }

    private fun setStatusBar() {
        immersionBar = ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .statusBarColor(R.color.black)
            .navigationBarColor(R.color.black)
            .fitsSystemWindows(true)
            .keyboardEnable(true)
        immersionBar?.init()
    }
}
